package com.qdsg.ysg.doctor.ui.dialog;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseDialogFragment;
import com.rest.business.RestProxy;
import com.rest.response.QueryProtocolResponse;

import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @time 2020/3/18 14:37
 */

public class RulesDialogFragment extends BaseDialogFragment {

    private ProgressBar progressBar;
    private TextView rulesTitle;
    private String rulesType;
    private Dialog mDialog;
    private OnItemClickListener mListener;
    private Button btn_commit;
    private WebView webView;


    public static RulesDialogFragment newInstance(String type) {
        Bundle bundle = new Bundle();
        bundle.putString( "rulesType", type );
        RulesDialogFragment rulesDialogFragment = new RulesDialogFragment();
        rulesDialogFragment.setArguments( bundle );
        return rulesDialogFragment;
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.dialog_rules, container );
        btn_commit = view.findViewById( R.id.btn_commit );
        rulesTitle = view.findViewById( R.id.title );
        progressBar = view.findViewById( R.id.progressBar );
        webView = view.findViewById( R.id.wv_rules_detail );
        assert getArguments() != null;
        rulesType = getArguments().getString( "rulesType" );

        if ("6".equals( rulesType )) {
            rulesTitle.setText( "预约规则" );
        } else if ("1".equals( rulesType )) {
            rulesTitle.setText( "知情同意书" );
        }else if("3".equals(rulesType)){
            rulesTitle.setText( "法律声明及隐私权政策" );
        }
        //可以设置webview没得 滚动条
        webView.setBackgroundColor( 0 );
//        webView.getBackground().setAlpha( 2 );
        webView.getSettings().setJavaScriptEnabled( false );
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        webView.getSettings().setTextZoom( 400);
        webView.setVerticalScrollBarEnabled( false );
        webView.setHorizontalScrollBarEnabled( false );
        webView.getSettings().setUseWideViewPort( true );//设置webview显示屏幕宽度 不能滑动
        webView.getSettings().setLoadWithOverviewMode( true );

        webView.setWebChromeClient( new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    if (webView != null) {
                        progressBar.setVisibility( View.GONE );//加载完网页进度条消失
                    }

                } else {
                    if (webView != null) {
                        progressBar.setVisibility( View.VISIBLE );//开始加载网页时显示进度条
                        progressBar.setProgress( newProgress );//设置进度值
                    }


                }
                super.onProgressChanged( view, newProgress );
            }
        } );

        btn_commit.setOnClickListener( v -> mListener.onConfirm() );

        queryProtocol();
        return view;
    }

    private void queryProtocol() {
        RestProxy.getInstance().queryProtocol( rulesType,new Observer<QueryProtocolResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(QueryProtocolResponse baseResponse) {
                webView.loadDataWithBaseURL( null, baseResponse.data.text, "text/html", "utf-8", null );
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        } );
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );


    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog( savedInstanceState );
        mDialog.setCancelable( false );
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity( Gravity.CENTER );
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Objects.requireNonNull( Objects.requireNonNull( getDialog() ).getWindow() ).requestFeature( Window.FEATURE_NO_TITLE );
        super.onActivityCreated( savedInstanceState );
        Objects.requireNonNull( getDialog().getWindow() ).setBackgroundDrawable( new ColorDrawable( 0x00000000 ) );
        getDialog().getWindow().setLayout( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT );
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


}
